package com.waitingresult.thread.pool;

import java.util.concurrent.*;

/**
 * All right reserved,Designed By WangShuai
 *
 * @author : WangShuai
 * @version : V1.0
 * @ClassName : ThreadPoolDemo1
 * @Description :
 * @date : 2022/9/22 16:40
 */
public class ThreadPoolDemo1 {
    private static final ExecutorService threadPoll =
            new ThreadPoolExecutor(8, 20, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));

    public static void main(String[] args) {
        extracted();
        //threadPoll.submit()
        threadPoll.execute(() -> {
            try {
                System.err.println("线程1执行" + Thread.currentThread().getName() + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        threadPoll.execute(() -> {
            try {
                System.err.println("线程2执行" + Thread.currentThread().getName() + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    private static void extracted() {
        // ExecutorService executor = Executors.newFixedThreadPool(3);
        // 涉及到顺序消费 可以使用当前 当前线程池只有一个线程
        ExecutorService executor = Executors.newSingleThreadExecutor();

        executor.execute(() -> {
            try {
                System.err.println("线程1执行" + Thread.currentThread().getName() + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.execute(() -> {
            try {
                System.err.println("线程2执行" + Thread.currentThread().getName() + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.shutdown();
    }
}